package com.itheima.a11threadsafe3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    //静态，表示这个类所有的对象，都共享ticket数据
    static int ticket = 0;

    static Lock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true) {
            lock.lock(); //同步代码块
            try {
                if (ticket < 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "@" + ticket);
                } else {
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }


        }
    }

}
